package com.company;

import com.company.service.JavaVmStackSOF;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Task1 {
    private static final int _1MB = 1024 * 1024;

    public static void main(String[] args) {
        //todo 1.编写代码，使之导致java堆溢出、虚拟机栈溢出、方法区溢出、和本地内存溢出；

        //堆溢出
        List<Test1> list = new ArrayList<>();
        while(true) {
            list.add(new Test1());
        }


        //虚拟机栈溢出
        /*JavaVmStackSOF sof = new JavaVmStackSOF();
        try {
            sof.stackLeak();
        } catch (Throwable throwable) {
            System.out.println("stack length: " + String.valueOf(sof.getStackLength()));
            throw throwable;
        }*/

        //方法区溢出
        /*while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(Test1.class);
            enhancer.setUseCache(false);
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                    return proxy.invokeSuper(obj, args);
                }
            });
            enhancer.create();
        }*/


        //本地内存溢出
        /*Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        while (true) {
            unsafe.allocateMemory(_1MB);
        }*/

    }

    static class Test1 {

    }
}
